package cn.bdqn.controller;

import cn.bdqn.pojo.BasDict;
import cn.bdqn.pojo.SalChance;
import cn.bdqn.pojo.User;
import cn.bdqn.service.BasDictService;
import cn.bdqn.service.SalChanceService;
import cn.bdqn.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chance")
public class SalChanceController {
    @Resource
    SalChanceService chanceService;
    @Resource
    BasDictService dictService;
    @Resource
    UserService userService;
    /**
     * 列表展示
     * @param name
     * @param title
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String name, String title,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model,HttpSession session) {
        //设置每页显示条数
        int pageSize = 3;
        //当前人id
        User user = (User)session.getAttribute("loginUser");
        //查询每页显示的数据
            IPage<SalChance> chancePager = chanceService.getChancesByPage(name,title, pageIndex, pageSize,user.getUsrId());
        model.addAttribute("chancePager", chancePager);
        //查询条件回显
        model.addAttribute("name",name);
        model.addAttribute("title",title);
        return "chance/list";
    }

    /**
     * 跳转新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model){
        List<BasDict> leveList = dictService.getDictByDictType("客户等级");
        model.addAttribute("levelList",leveList);

        List<BasDict> regionList = dictService.getDictByDictType("地区");
        model.addAttribute("regionList",regionList);

        //查客户经理
        List<User> usersByRoleId = userService.getUsersByRoleId(2l);
        model.addAttribute("userList",usersByRoleId);
        return "chance/add";
    }

    @RequestMapping("/save")
    public String save(SalChance chance, HttpSession session) {
        User user = (User)session.getAttribute("loginUser");

        try {
            chance.setChcCreateBy(user.getUsrName());
            chance.setChcCreateId(user.getUsrId());
            chance.setChcCreateDate(new Date());
            chance.setChcDueDate(new Date());

            if (chance.getChcDueId()!=null){
                chance.setChcStatus("2");
            }else{
                //没有指派
                chance.setChcStatus("1");

            }
            chanceService.addChance(chance);

            //跳转到列表页面
            return "redirect:/chance/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/chance/add";
        }
    }

    /**
     * 跳转到修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long id, Model model) {
        SalChance chance = chanceService.getChance(id);
        model.addAttribute("chance",chance);
        //2.查询客户等级
        List<BasDict> levelList=dictService.getDictByDictType("客户等级");
        model.addAttribute("levelList",levelList);
        //3.查询地区
        List<BasDict> regionList=dictService.getDictByDictType("地区");
        model.addAttribute("regionList",regionList);
        //4.查询客户经理
        List<User> userList=userService.getUsersByRoleId(2L);
        model.addAttribute("userList",userList);
        return "chance/edit";
    }
    /**
     * 修改操作
     * @param chance
     * @return
     */
    @RequestMapping("/update")
    public String update(SalChance chance) {
        try {
            if (chance.getChcDueId()!=null){
                chance.setChcStatus("2");
            }else{
                chance.setChcStatus("1");
            }
            chanceService.updateChance(chance);
            return "redirect:/chance/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/chance/edit?id="+chance.getChcId();
        }
    }
    /**
     * 删除操作
     * @param id
     * @param session
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Long id, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            //查询销售机会
            SalChance chance = chanceService.getChance(id);
            //判断销售机会状态,是否为未分配
            if (!chance.getChcStatus().equals("1")) {
                map.put("delResult", -1);//销售机会不是未分配，不能删除
                return map;
            }
            //判断当前删除数据的人，是否为销售机会创建人
            User loginUser = (User) session.getAttribute("loginUser");
            if (!loginUser.getUsrId().equals(chance.getChcCreateId())) {
                map.put("delResult", -2);//删除人不是销售机会创建人，不允许删除
                return map;
            }
            chanceService.deleteChance(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }
    /**
     * 跳转指派销售机会页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toAssign")
    public String toAssign(Long id,Model model){
        //1.查询客户经理列表
        List<User> userList=userService.getUsersByRoleId(2L);
        model.addAttribute("userList",userList);
        //2.根据销售机会ID，查询信息
        SalChance chance=chanceService.getChance(id);
        model.addAttribute("chance",chance);
        return "chance/assign";
    }

    /**
     * 保存指派的信息
     * @param chance
     * @return
     */
    @RequestMapping("/assign")
    public String assign(SalChance chance) {
        //指派时间
        chance.setChcDueDate(new Date());
        //修改销售机会的状态
        chance.setChcStatus("2");
        //修改销售机会的指派人员
        chanceService.updateChance(chance);
        try {
            return "redirect:/chance/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到指派页面
            return "redirect:/chance/assign?id="+chance.getChcId();
        }
    }


}
